var app = getApp()
var quanModel = require("../../model/quanModel.js")
var userUtil = require("../../utils/userUtil.js")
var util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    quan: {},
    customerQuanRole:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.scene) {
      var scene = decodeURIComponent(options.scene)
      this.init(scene)
    } else {
      this.setData({
        quan: app.data.quan
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (userUtil.isLogin()) {
      var that=this
      quanModel.getCustomerQuanRole(this.data.quan.id,res=>{
        if(res.status==0)
          that.setData({
            customerQuanRole:res.data
          })
      })
    }
  },
  
  init(id) {
    var that=this
    util.showLoading("加载中")
    quanModel.getQuanInfo(id,res=>{
      wx.hideLoading()
      if(res.status==1){
        util.failToast(res.msg)
        return
      }
      that.setData({
        quan:res.data
      })
    })
  },

  //领取优惠券
  bindGetQuan() {
    if (!userUtil.isLogin()) {
      userUtil.toLogin()
      return
    }
    var quan = this.data.quan
    var data = {
      type: quan.type,
      quanId: quan.id,
      quanName: quan.name,
      etime: quan.etime
    }
    quanModel.addQuan(data, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      util.successToast(res.msg)
      setTimeout(() => {
        wx.switchTab({
          url: '../nav_home/nav_home',
        })
      }, 1000)
    })
  },

})